package com.wfcm.controller;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import com.wfcm.utils.ISsoLoginHelper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.wfcm.annotation.IgnoreToken;
import com.wfcm.entity.WfCardCourseRelationEntity;
import com.wfcm.entity.WfCollectionEntity;
import com.wfcm.entity.WfCourseInfoEntity;
import com.wfcm.entity.WfShoppingCartEntity;
import com.wfcm.service.WfCollectionService;
import com.wfcm.service.WfCourseChapterService;
import com.wfcm.service.WfCourseInfoService;
import com.wfcm.service.WfShoppingCartService;
import com.wfcm.utils.R;
import com.wfcm.utils.WebUtils;

/**
 * 
 * @content 收藏Controller
 */
@RequestMapping("/favorite")
@Controller
public class FavoriteController {
	private Logger log = LoggerFactory.getLogger(AdInfoController.class);
	@Autowired
	WfCourseChapterService courseChapterService;
	@Autowired
	WfCourseInfoService courseInfoService;
	@Autowired
	private WfCollectionService collectionService;
	
	/**
	 * 收藏展示接口
	 */
	@RequestMapping("/list")
	@ResponseBody
	public R goods(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
		//type商品类型（1.课程 2.章节3.vip卡4商品）
		String type = req.getParameter("type");
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		map.put("type", type);
		
		List<WfCollectionEntity> resultList = new ArrayList<WfCollectionEntity>();
		List<WfCollectionEntity> collectionList = collectionService.queryList(map);
		if("1".equals(type)) {//课程
			for(int i=0 ; i<collectionList.size(); i++) {
				map.put("foreignId", collectionList.get(i).getForeignid());
				List<WfCollectionEntity> courseList =	collectionService.queryCourseList(map);
				resultList.add(courseList.get(0));
			}
		}else if("2".equals(type)) {//章节
			for(int i=0 ; i<collectionList.size(); i++) {
				map.put("foreignId", collectionList.get(i).getForeignid());
				List<WfCollectionEntity> chapterList =	collectionService.queryChapterList(map);
				resultList.add(chapterList.get(0));
			}
		}else if("3".equals(type)) {//课程包
			for(int i=0 ; i<collectionList.size(); i++) {
				map.put("foreignId", collectionList.get(i).getForeignid());
				List<WfCollectionEntity> vipCardList =	collectionService.queryVipCardList(map);
				resultList.add(vipCardList.get(0));
			}
		}
		Map<String,Object> resultMap = new HashMap<String,Object>();
		resultMap.put("resultList", resultList);
		
		return R.ok(resultMap);
	}	
	
	/**
	 * 删除收藏
	 */
	@RequestMapping("/del")
	@ResponseBody
	public R delGoods(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
	
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("userId", userId);
		
		String collectionId = req.getParameter("collectionId");
		String[] result = collectionId.split(",");
		
		String type = req.getParameter("type");
		String[] typeList = type.split(",");
		
		for(int i=0; i<result.length; i++) {
			int id = Integer.parseInt(result[i]);
			int t =  Integer.parseInt(typeList[i]);
			map.put("foreignId", id);
			map.put("type", t);
			collectionService.delete(map);
		}
		
		return R.ok();
	}	
	
	/**
	 * 加入或删除收藏
	 */
	@RequestMapping("/add")
	@ResponseBody
	public R addGoods(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
		//type商品类型（1.课程 2.章节3.vip卡4商品）
		String type = req.getParameter("type");
		String foreignId = req.getParameter("foreignId");//商品id
		int c = 0;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", type);
		map.put("userId", userId);
		map.put("foreignId", foreignId);
		List<WfCollectionEntity> collectionEntityList = collectionService.queryCollectionList(map);
		if(collectionEntityList.size() == 0) {
			WfCollectionEntity collection = new WfCollectionEntity();
			collection.setUserid(userId);
			collection.setType(Integer.valueOf(type));
			collection.setForeignid(Integer.valueOf(foreignId));
			collection.setCreateTime(new Date());
			collectionService.save(collection);
			c = 1;//已加入收藏
		}else {
			collectionService.delete(map);
			c = 0;//已删除收藏
			/*return R.error(51, "您已经加入收藏了");*/
		}
		return R.ok().put("collection", c);
	}	
	
	/**
	 * 加入或删除收藏
	 */
	@RequestMapping("/iosAdd")
	@ResponseBody
	public R iosAdd(HttpServletRequest req) {
		Integer userId = ISsoLoginHelper.confirm(req);
		//type商品类型（1.课程 2.章节3.vip卡4商品）
		String type = req.getParameter("type");
		String foreignId = req.getParameter("foreignId");//商品id
		int c = 0;
		Map<String,Object> map = new HashMap<String,Object>();
		map.put("type", type);
		map.put("userId", userId);
		map.put("foreignId", foreignId);
		List<WfCollectionEntity> collectionEntityList = collectionService.queryCollectionList(map);
		if((collectionEntityList).size() == 0) {
			WfCollectionEntity collection = new WfCollectionEntity();
			collection.setUserid(userId);
			collection.setType(Integer.valueOf(type));
			collection.setForeignid(Integer.valueOf(foreignId));
			collection.setCreateTime(new Date());
			collectionService.save(collection);
			c = 1;//已加入收藏
		}else {
			return R.error(51, "您已经加入收藏了");
		}
		return R.ok().put("collection", c);
	}	
	
}
